//
//  PayDetailController.m
//  WuJieLive
//
//  Created by iMac on 2017/11/21.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "PayDetailController.h"
#import "MyWalletCell.h"

@interface PayDetailController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong) UITableView * tableview;
@property (nonatomic , strong) NSMutableArray * dataArray;
@property (nonatomic , assign) NSInteger index;

@end

@implementation PayDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)setUI
{
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 220 - 50 - 64 ) style:(UITableViewStylePlain)];
    [self.tableview registerNib:[UINib nibWithNibName:@"MyWalletCell" bundle:nil] forCellReuseIdentifier:@"MyWalletCell"];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.tableFooterView = [UIView new];
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
    [self.tableview.mj_header beginRefreshing];
    self.tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestMoreData)];
    [self.view addSubview:self.tableview];
}

- (void)requestData
{
    self.index = 1;
    [self requestMoreData];
}

-(void)requestMoreData
{
    NSDictionary * dict = @{@"token":self.userToken,@"type":@"out",@"page":[NSString stringWithFormat:@"%ld",self.index]};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@deposit-account/wallet-consume-lists",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        if (isSuccess) {
            if (self.index == 1) {
                self.dataArray = [NSMutableArray array];
            }
            for (int i = 0; i < [responseObject[@"data"] count]; i ++) {
                WalletDetailModel * model = [[WalletDetailModel alloc]init];
                [model setValuesForKeysWithDictionary:[responseObject objectForKey:@"data"][i]];
                [self.dataArray addObject:model];
            }
            if ([responseObject[@"hasmore"] integerValue] == 0) {
                [self.tableview.mj_footer endRefreshingWithNoMoreData];
            }
            self.index += 1;
            [self.tableview reloadData];
            [self endRefresh];
        }
    } failure:^(NSError *error) {
        [self endRefresh];
    }];
}
- (void)endRefresh
{
    if (self.tableview.mj_header.isRefreshing) {
        [self.tableview.mj_header endRefreshing];
    }else
    {
        [self.tableview.mj_footer endRefreshing];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyWalletCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MyWalletCell" forIndexPath:indexPath];
    [cell bindDataWithModel:self.dataArray[indexPath.row]];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 65;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    WalletDetailController * vc = [[WalletDetailController alloc]init];
//    [self.navigationController pushViewController:vc animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
